﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Noris.Tools.FrxEditor.Data
{
    public class SimpleDataGrid : DataGridView
    {
        public SimpleDataGrid()
        {
            this.InitializeProperty();
        }
        private void InitializeProperty()
        {
            this.AllowDrop = false;
            this.AllowUserToAddRows = false;
            this.AllowUserToDeleteRows = false;
            this.AllowUserToOrderColumns = true;
            this.AllowUserToResizeColumns = true;
            this.AllowUserToResizeRows = false;
            this.AutoGenerateColumns = false;
            this.AutoSize = false;
            this.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            this.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
            this.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
            this.ColumnHeadersHeight = 45;
            this.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
            this.ColumnHeadersVisible = true;
            this.EnableHeadersVisualStyles = true;
            this.MultiSelect = true;
            this.ReadOnly = true;
            this.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.ShowEditingIcon = false;
            this.StandardTab = false;
        }

        public SimpleDataTable DataTable
        {
            get { return this._DataTable; }
            set
            {
                this._DataTable = value;
                this.ReloadContent();
            }
        }
        private SimpleDataTable _DataTable;

        private void ClearData()
        {
            this.Rows.Clear();
            this.Columns.Clear();
        }
        private void ReloadContent()
        {
            this.ClearData();

            SimpleDataTable table = this._DataTable;
            if (table == null) return;
            if (table.ColumnCount == 0) return;
            this.LoadColumns(table.Columns);
            this.LoadRows(table.Rows);

        }
        private void LoadColumns(SimpleDataColumnCollection columns)
        {
            foreach (SimpleDataColumn column in columns)
            {
                DataGridViewColumn dgCol = new DataGridViewColumn();
                dgCol.Name = column.ColumnName;
                dgCol.HeaderText = column.ColumnName;
                dgCol.DefaultCellStyle = this.DefaultCellStyle;
                this.Columns.Add(dgCol);
            }
        }
        private void LoadRows(SimpleDataRowCollection rows)
        {
            foreach (SimpleDataRow row in rows)
            {
                object[] values = row.Values;

                DataGridViewRow dgRow = new DataGridViewRow();
                for (int c = 0; c < values.Length; c++)
                {
                    DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
                    cell.Value = values[c];
                    dgRow.Cells.Add(cell);
                }
                this.Rows.Add(dgRow);
            }
        }
    }
}
